package com.xiaoshuidi.cloud.module.contract.controller.admin.bill;

import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.framework.excel.core.util.ExcelUtils;
import com.xiaoshuidi.cloud.framework.operatelog.core.annotations.OperateLog;
import com.xiaoshuidi.cloud.module.contract.controller.admin.bill.vo.OrderInformationVo;
import com.xiaoshuidi.cloud.module.contract.controller.admin.bill.vo.orderFees.*;
import com.xiaoshuidi.cloud.module.contract.dal.dataobject.bill.OrderFeesDO;
import com.xiaoshuidi.cloud.module.contract.enums.ApiConstants;
import com.xiaoshuidi.cloud.module.contract.service.bill.OrderFeesService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static com.xiaoshuidi.cloud.framework.common.pojo.CommonResult.success;
import static com.xiaoshuidi.cloud.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 账单明细")
@RestController
@RequestMapping(ApiConstants.NAME + "/fee")
@Validated
public class OrderFeesController {

    @Resource
    private OrderFeesService orderFeesService;

    @PostMapping("/create")
    @Operation(summary = "创建账单明细")
    public CommonResult<Long> createContractOrderfees(@Valid @RequestBody OrderFeesCreateReqVO createReqVO) {
        return success(orderFeesService.createOrderFees(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新账单明细")
    public CommonResult<Boolean> updateContractOrderfees(@Valid @RequestBody OrderFeesUpdateReqVO updateReqVO) {
        orderFeesService.updateContractOrderfees(updateReqVO);
        return success(true);
    }

    @PostMapping("/batchUpdate")
    @Operation(summary = "批量更新账单明细")
    public CommonResult<Boolean> batchUpdate(@Valid @RequestBody List<OrderFeesDO> updateReqVOS) {
        orderFeesService.batchUpdate(updateReqVOS);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除账单明细")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteContractOrderfees(@RequestParam("id") Long id) {
        orderFeesService.deleteContractOrderfees(id);
        return success(true);
    }

    @GetMapping("/order-detail")
    @Operation(summary = "通过账单id 获得账单明细列表")
    @Parameter(name = "orderId", description = "账单id", required = true, example = "7422")
    public CommonResult<OrderFeesDetailVo> orderDetail(@RequestParam("id") Long id) {
        OrderFeesDetailVo billDetails = orderFeesService.getOrderFeeDetail(id);

        return success(billDetails);
    }

    @GetMapping("/get")
    @Operation(summary = "通过账单明细id 获得单个账单明细")
    @Parameter(name = "orderId", description = "账单明细id", required = true, example = "7422")
    public CommonResult<OrderInformationVo> getOrderFeesById(@RequestParam("id") Long id) {
        OrderInformationVo billDetails = orderFeesService.getOrderFeesById(id);
        return success(billDetails);
    }

    @GetMapping("/get-order-fees-detail")
    @Operation(summary = "查询账单明细")
    @Parameter(name = "orderId", description = "账单id", required = true, example = "7422")
    public CommonResult<PageResult<OrderDetailVo>> getOrderFeesDetail(OrderFeesDetailReq req) {
        PageResult<OrderDetailVo> billDetails = orderFeesService.getOrderFeesDetail(req);
        return success(billDetails);
    }

    /**
     * 导出租客账单明细
     * */
    @GetMapping("/export-order-fees-detail")
    @Operation(summary = "导出租客账单明细 Excel")
//    @PreAuthorize("@ss.hasPermission('contract:export')")
    @OperateLog(type = EXPORT)
    public void exportContractExcel(OrderFeesDetailReq req,HttpServletResponse response) throws IOException {
        List<OrderDetailExcelVo> billDetails = orderFeesService.exportContractExcel(req);
        // 导出 Excel
        ExcelUtils.write(response, "租客合同.xls", "数据", OrderDetailExcelVo.class, billDetails);
    }
}
